<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\cache\driver\Redis;
use think\Db;
use think\Request;

/**
 * 齐悟sdk
 */
class Ai extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    //1.用户出题，图灵匹配题目+标识---请求启悟问一个问题
    //2.用户答题---可到结束
    public function index(){
        $redis = new Redis();
        $msg = $this->request->post('msg');
        $uid = $this->request->post('uid');
        $Qiwu = new Qiwu();
        if($redis->has("type_".$uid)){//问答进行中
//            $flag = session('flag_'.$uid);
            $flag = $redis->get('flag_'.$uid);
            $msg = $flag.'⇛'.$msg.'⇚';
            $qiwures = $Qiwu->get_qiwu_api($msg,$uid);
            /*echo '问答进行中----';
            echo $msg;
            echo '----';*/
            if($qiwures['data']['botStatus']=='enter'){//
                $qiwures['next']=1;
                $redis->rm('flag_'.$uid);
                $redis->rm('type_'.$uid);
                /*session('flag_'.$uid,'');
                session('type_'.$uid,'');*/
                $this->success('问题返回',array('qiwu'=>$qiwures,'next'=>1));
            }else{
                $qiwures['next']=2;
                $redis->set('flag_'.$uid,$flag,'300');
                $redis->set('type_'.$uid,1,'300');
            }
            $this->success('问题返回',$qiwures);
        }else{
            $Turingos = new Turingos();
            $res = $Turingos->index($msg);//去图灵匹配题目
            if($res['code']==1){
                $arr = explode('+',$res['data']['title']);
                $flag = $arr[0];
                $title = $arr[1];
                $msg = $flag.'⇛⇨'.$title.'⇦⇚';
               /* echo '题目';
                dump($msg);*/
                $redis->set('flag_'.$uid,$flag,'300');
                $redis->set('type_'.$uid,1,'300');
                $qiwures = $Qiwu->get_qiwu_api($msg,$uid);
                $qiwures['next']=2;
                $this->success('问题返回',$qiwures);
            }else{
                $redis->rm('flag_'.$uid);
                $redis->rm('type_'.$uid);
                /*session('flag_'.$uid,'');
                session('type_'.$uid,'');*/
                $this->success('tuling知识库未匹配到',array('next'=>1));//1题目
            }
        }
    }

    //语音转文字
    public function voicetotext(){
        $Turingos = new Turingos();
        $file = $_FILES['file'];
        $deviceId = $this->request->post('deviceId');
        if(!empty($file)){
            $res = $Turingos->speechasr($deviceId,$file);
            if($res['code']==1){
                $this->success('语音转文本成功',['text'=>$res["text"]]);
            }else{
                $this->error('语音转文本失败，请联系管理员');
            }
        }else{
            $this->error('语音文件不能为空');
        }
    }

    //文字转语音
    public function texttovoice(){
        $Turingos = new Turingos();
        $msg = $this->request->post('msg');
        $deviceId = $this->request->post('deviceId');
        if(!empty($msg)){
            $res = $Turingos->speechtts($deviceId,$msg);
            if($res['code']==1){
                $this->success('文本转语音成功',['url'=>$res["url"]]);
            }else{
                $this->error('文本转语音失败，请联系管理员');
            }
        }else{
            $this->error('文本内容不能为空');
        }
    }

    //动画列表
    public function animationlist(){
        $list = Db::name('animation')
            ->select();
        $request = Request::instance();
        $domain = $request->domain();
        foreach ($list as $key=>$val){
            $list[$key]['image'] = $domain.$list[$key]['image'];
        }
        $this->success('获取成功',$list);
    }


}
